import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:sptapp/moudle/common_info.dart';

import '../../api/interface/login_api.dart';
import '../../api/request/response_data.dart';
import '../../config/const.dart';
import '../../moudle/event_bus.dart';
import '../../moudle/message.dart';
import '../../widget/common_widget.dart';

class MinInfoChangeNickname extends StatefulWidget {
  const MinInfoChangeNickname({super.key});

  @override
  State<MinInfoChangeNickname> createState() => _MinInfoChangeNicknameState();
}

class _MinInfoChangeNicknameState extends State<MinInfoChangeNickname> {

  TextEditingController accountController = TextEditingController();
  String? account;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    accountController.text = baseUserInfo['nickname']??'';
    print('用户名==='+baseUserInfo.toString());
    account = accountController.text;
  }

  _register()async{
    if(account == null || account!.isEmpty){
      return;
    }
    Map<String,dynamic> data = {
      'nickname':account,
    };
    showLoading();
    ResponseData res = await LoginAPI.userEdit(data);
    hideLoading();
    if(res.code == 200){
      baseUserInfo['nickname'] = account;
      getBaseUserInfo((){});
      Navigator.of(context).pop();
    }
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: defaultBlackBgColor,
      body: GestureDetector(
        onTap: (){
          FocusScope.of(context).requestFocus(FocusNode());
        },
        child: Container(
          padding: EdgeInsets.symmetric(horizontal: 48.w),
          child: Column(
            children: [
              SizedBox(height: ScreenUtil().statusBarHeight + 40.w,),
              myCustomCenterTitleAppBar('Edit username'),
              SizedBox(height: 60.w,),
              Container(
                width: 654.w,
                height: 108.w,
                padding: EdgeInsets.symmetric(horizontal: 30.w),
                decoration: BoxDecoration(
                    border: Border.all(color: Color.fromRGBO(255, 255, 255, 0.5),width: 1.w),
                    borderRadius: BorderRadius.circular(20.w)
                ),
                child:  Row(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Expanded(child: TextField(
                      //控制器
                      textInputAction: TextInputAction.done,
                      controller: accountController,
                      maxLength: 20,
                      maxLines: 1,
                      //是否自动更正
                      autocorrect: true,
                      //是否自动对焦
                      autofocus: false,
                      style: inputStyle,
                      inputFormatters: [

                      ],
                      decoration: InputDecoration(
                        contentPadding: const EdgeInsets.symmetric(vertical: 0,horizontal: 0),
                        hintText: user_name_hinText.tr,
                        hintStyle: inputHitStyle,
                        counterText: "",
                        border: OutlineInputBorder(
                            borderSide: BorderSide.none),
                      ),
                      onChanged: (val){
                        setState(() {
                          account = val.trim();
                        });
                      },
                    )),
                    SizedBox(width: 20.w,),
                    Visibility(visible: account!=null&&account!.isNotEmpty,
                      child: GestureDetector(
                        onTap: (){
                          setState(() {
                            accountController.text = '';
                            account = null;
                          });
                        },
                        child: Container(
                          child: inputImg,
                        ),
                      ),
                    )
                  ],
                ),
              ),
              SizedBox(height: 48.w,),
              GestureDetector(
                onTap: (){
                  FocusScope.of(context).requestFocus(FocusNode());
                  _register();
                },
                child: Container(
                  child: Text('Done',style: TextStyle(
                      color: Colors.white,
                      fontSize: 28.sp
                  ),),
                  decoration: BoxDecoration(
                      color: account != null && account!.isNotEmpty ? themeColor : themeDisableColor,
                      borderRadius: BorderRadius.circular(8.w)
                  ),
                  height: 108.w,
                  alignment: Alignment.center,
                ),
              ),

            ],
          ),
        ),
      ),
    );
  }
}
